package com.qms.repast.net;


import com.orhanobut.logger.Logger;
import com.qms.repast.App;
import com.qms.repast.commons.Constants;
import com.qms.repast.service.IUserService;
import com.qms.repast.utils.SystemUtils;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @author 泅渡者
 * @date 2018/6/6 0006.
 * @describe:
 */

public class MainRetrofit {
    
    public IUserService userService;
    
    public static String BASE_URL = Constants.getBaseUrl();
    
    private static final int DEFAULT_TIMEOUT = 5;
    
    private static Retrofit retrofit;
    
    private static OkHttpClient client;
    
    public MainRetrofit() {
        provideOkHttp();
        this.userService = retrofit.create(IUserService.class);
    }
    
    public static void provideOkHttp() {
        HttpsUtils.SslParams sslParams = HttpsUtils.getSslSocketFactory();
        client = new OkHttpClient.Builder()
                .addNetworkInterceptor(new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
                    @Override
                    public void log(String message) {
                        Logger.d(message);
                    }
                }).setLevel(HttpLoggingInterceptor.Level.BODY))
                .sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager)
                .addInterceptor(getInterceptor())
                .connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                .cache(getCacheInstance())
                .build();
        provideRetrofit();
    }
    
    
    public static Interceptor getInterceptor() {
        return new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request request = chain.request()
                        
                        .newBuilder()
                        
                        .addHeader("session", SystemUtils.getSession(App.getContext()))
                        
                        .addHeader("clientID", SystemUtils.getMyUUID(App.getContext()))
                        
                        .addHeader("registerID", SystemUtils.getRegisterId(App.getContext()))
                        
                        .addHeader("clientV", SystemUtils.getVersionName(App.getContext()))
                        
                        .addHeader("User-Agent", SystemUtils.getUserAgent(App.getContext()))
                        
                        .addHeader("deviceId", SystemUtils.getDeviceId())
                        
                        .build();
                
                return chain.proceed(request);
            }
        };
    }
    
    public static Cache getCacheInstance() {
        //设置 请求的缓存
        File cacheFile = new File(App.getInstance().getCacheDir(), "cache");
        //50Mb
        Cache cache = new Cache(cacheFile, 1024 * 1024 * 50);
        return cache;
    }
    
    public static void provideRetrofit() {
        retrofit = new Retrofit.Builder()
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .baseUrl(BASE_URL)
                .build();
    }
    
    public IUserService getIUserService() {
        return this.userService;
    }
}
